package com.example.it.recycleviewpager;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private AdvertisingAdapter advertisingAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager) this.findViewById(R.id.vp);
        init();
    }

    void init() {
        List<Integer> resIdList = new ArrayList<>();
        resIdList.add(R.drawable.pet2);
        resIdList.add(R.drawable.pet3);
        resIdList.add(R.drawable.pet4);
        advertisingAdapter = new AdvertisingAdapter(this, resIdList);
        viewPager.setAdapter(advertisingAdapter);
        //设置第二张图片为最初显示的图片
        viewPager.setCurrentItem(1);
        //设置Item的选中事件，实现循环
        viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                if (position == 0)
                    viewPager.setCurrentItem(viewPager.getAdapter().getCount() - 2, false);
                if (position == viewPager.getAdapter().getCount() - 1)
                    viewPager.setCurrentItem(1, false);
            }
        });
    }
}
